package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.common.app.R
import com.common.app.databinding.DialogEmulatorCheckLayoutBinding
import com.common.base.utils.ViewUtils

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：13/7/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class EmulatorCheckDialog(context: Context) :
    Dialog(context), View.OnClickListener {

    private var binding: DialogEmulatorCheckLayoutBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnEmulatorCheckDialogListener? = null

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        setDialogTheme()
        setCanceledOnTouchOutside(false)
        mContext = context
    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogEmulatorCheckLayoutBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        binding?.exitApp?.setOnClickListener(this)
    }

    override fun onClick(view: View) {

        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        binding?.apply {
            if (exitApp == view) {
                listener?.onEmulatorCheckDialogClick()
                dismiss()
            }
        }
    }

    override fun onBackPressed() {
        //强制更新时,按返回键不隐藏dialog,方法体置空即可
    }

    override fun show() {
        //获取当前Activity所在的窗体
        val dialogWindow = window
        //设置Dialog从窗体底部弹出
        dialogWindow?.setGravity(Gravity.CENTER)
        dialogWindow?.decorView?.setPadding(55, 0, 55, 0)
        val lp = dialogWindow?.attributes
        //设置宽
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        //设置高
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialogWindow?.attributes = lp
        //显示对话框
        dialogWindow?.setGravity(Gravity.CENTER)
        //设置dialog的动画效果
        dialogWindow?.setWindowAnimations(R.style.bottomShowStyle)
        super.show()
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnEmulatorCheckDialogListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnEmulatorCheckDialogListener {
        fun onEmulatorCheckDialogClick()
    }
}